//package com.btobtob.a.biz.user.commonweb.exception;
//
//import com.alibaba.cola.dto.Response;
//import com.alibaba.cola.exception.BizException;
//import com.alibaba.cola.exception.SysException;
//import com.alibaba.fastjson.JSONObject;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.validation.ConstraintViolation;
//import jakarta.validation.ConstraintViolationException;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.http.ResponseEntity;
//import org.springframework.validation.BindingResult;
//import org.springframework.validation.ObjectError;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.RestControllerAdvice;
//
//import java.util.List;
//import java.util.Set;
//
//@Slf4j
//@RestControllerAdvice
//@ResponseBody
//public abstract class BaseGlobalExceptionHandler {
//
//    @ExceptionHandler(BizException.class)
//    public Response bizExceptionHandler(BizException exception, HttpServletRequest request) {
//        return Response.buildFailure(exception.getErrCode(), exception.getMessage());
//    }
//
//    @ExceptionHandler(SysException.class)
//    public Response sysExceptionHandler(SysException exception, HttpServletRequest request) {
//        return Response.buildFailure(exception.getErrCode(), exception.getMessage());
//    }
//
//    @ExceptionHandler(RuntimeException.class)
//    public Object runtimeExceptionHandler(RuntimeException exception, HttpServletRequest request) {
//
//        boolean isDubboException = false;
//        StackTraceElement[] stackArray = exception.getStackTrace();
//        for (int i = stackArray.length-1; i >= 0; i--) {
//            StackTraceElement element = stackArray[i];
//            if(element.toString().contains("org.apache.dubbo.")){
//                isDubboException = true;
//            }
//        }
//        if(isDubboException){ //已经在远程provider处记录日志，这里不记录日志，只在控制台显示。
//            exception.printStackTrace();
//            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//        else{
//            log.error(exception.getMessage(), exception);
//            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }
//
//    /**
//     * dubbo参数验证异常
//     * @param request
//     * @param ex
//     * @return
//     */
//    @ExceptionHandler(value = ConstraintViolationException.class)
//    public Object constraintViolationExceptionHandler(HttpServletRequest request, ConstraintViolationException ex) {
//        JSONObject jo = new JSONObject();
//        jo.put("msg",ex.toString());
//        ConstraintViolationException exs = (ConstraintViolationException) ex;
//        Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
//        for (ConstraintViolation<?> item : violations) {
//            jo.put(item.getPropertyPath().toString(), item.getMessage());
//        }
//        HttpHeaders httpHeaders = new HttpHeaders();
//        MediaType mediaType = MediaType.APPLICATION_JSON_UTF8;
//        httpHeaders.setContentType(mediaType);
//        return new ResponseEntity<String>(jo.toJSONString(), httpHeaders, HttpStatus.BAD_REQUEST);
//    }
//
//    private String handlerErrors(BindingResult bindingResult){
//        List<ObjectError> errors = bindingResult.getAllErrors();
//        ObjectError error = errors.get(0);
//        String msg = error.getDefaultMessage();
//        return msg;
//    }
//}
